#include <iostream>
#include <jsoncpp/json/json.h>

#include "../com/httplib.h"
#include "compile_run.hpp"
using namespace lh_com_run;
using namespace httplib;
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "use : " << argv[0] << " port" << std::endl;
    }
    int port = atoi(argv[1]);
    Server svr;
    // svr.Get("/test",[](const Request& req,Response& res){
    //    res.set_content("hello httplib, 我爱你 ","text/plain;charset=utf-8");
    // });
    // Request请求中，正文部分内容是json串
    svr.Post("/compile_run", [](const Request &req, Response &res)
             {
        std::string in_json = req.body;
        if(!in_json.empty())
        {
            // std::cout<<"进入编译运行服务"<<std::endl;
            std::string out_json;
            CompileRun::start(in_json,&out_json);
            // std::cout <<"out_json : "<< out_json <<std::endl;
            res.set_content(out_json,"application/json;charset=utf-8");
        } });
    svr.listen("0.0.0.0", port);
    return 0;
}

// int main()
// {
//     Json::Value in_val;
//     in_val["code"] = R"(#include <iostream>
//         int main(){
//             std::cout<<"Can I trust you ?"<<std::endl;
//             return 0;
//         })";
//     in_val["input"] = "";
//     in_val["cpu_limit"] = 1;
//     in_val["mem_limit"] = 10240 * 5;

//     Json::FastWriter writer;
//     std::string in_json = writer.write(in_val);

//     std::string out_json;
//     CompileRun::start(in_json, &out_json);
//     std::cout << out_json << std::endl;
//     // 反序列化
//     Json::Reader reader;
//     Json::Value out_val;
//     reader.parse(out_json, out_val);
//     std::string reason = out_val["reason"].asString();
//     int status = out_val["status"].asInt();
//     std::string out = out_val["stdout"].asString();
//     std::string err = out_val["stderr"].asString();
//     std::cout << status << std::endl;
//     std::cout << reason << std::endl;
//     std::cout << out << std::endl;
//     std::cout << err << std::endl;
//     return 0;
// }